package com.cjm.algorithm.leetcode;

import com.cjm.algorithm.ListNode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class TwoHeLinked {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int cat = 0;
        ListNode left = l1;
        ListNode right = l2;
        ListNode reslut = new ListNode(0);
        while (left != null || right != null) {
            int sum = cat;
            if (left != null) {
                sum += left.val;
                left = left.next;
            }
            if (right != null) {
                sum += right.val;
                right = right.next;
            }
            cat = sum/10;
            reslut.next = new ListNode(sum % 10);
            reslut = reslut.next;
        }
        if (cat >  0) {
            reslut.next = new ListNode(cat);
        }
        return reslut.next;
    }
}
